//
//  DJBannerView.m
//  Gunterapple
//
//  Created by 热带雨林 on 2022/1/15.
//  Copyright © 2022 热带雨林. All rights reserved.
//

#import "DJBannerView.h"

#pragma mark - 声明全局的定时器
static NSTimer *DJ_timer;

@interface DJBannerView ()<UIScrollViewDelegate>{
    //记录传入的图片大小尺寸
    CGFloat DJ_width;
    CGFloat DJ_height;
}

@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) UIScrollView *scrollView;

@end

@implementation DJBannerView
#pragma mark - 构造函数
- (id)initWithFrame:(CGRect)frame  withImageDataArray:(NSMutableArray *)imageDataArray{
    self = [super initWithFrame:frame];
    //判断外界传入的图片是否是多于一张，如果就是一张的话，就没有滚动的必要
    if(self && imageDataArray.count > 2){
        DJ_width = frame.size.width;
        DJ_height = frame.size.height;
        self.dataArray = [NSMutableArray arrayWithArray:imageDataArray];
        //如果是使用scrollView创建轮播图的话，注意，需要对数组进行操作
        //假设图片数组为 1 2 3 需要在本地数组的末尾添加1 在本地的数组首位添加3  最后变为 3 1 2 3 1
        [self.dataArray addObject:imageDataArray.firstObject];
        [self.dataArray insertObject:imageDataArray.lastObject atIndex:0];
        //添加scrollView
        
        //添加pageControll
        
        //添加定时器
        
    }
    return self;
}
#pragma mark - ScrollView代理方法
//scrollView刚开始被手动拖拽时调用
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    /**
     判断是否存在定时器 注意：如果有定时器的话，需要先将定时器停掉
     */
    if(DJ_timer){
        [DJ_timer setFireDate:[NSDate distantFuture]];
    }
}
//scrollView被手动拖拽过程中调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
}
//scrollView完成减速时调用，也就是手动拖拽完成
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
}

#pragma mark - Lazy
- (void)addTimer{
    //初始化timer
    DJ_timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(timerFunc:) userInfo:nil repeats:YES];
    //加到runloop中
    [[NSRunLoop currentRunLoop]addTimer:DJ_timer forMode:NSRunLoopCommonModes];
}
//
- (void)timerFunc:(NSTimer *)timer{
    //获取当前X的位置
    CGFloat currentX = _scrollView.contentOffset.x;
    //获取即将要展示的下一张的图片的位置
    CGFloat nextX = currentX + DJ_width;
    //判断是否要是显示最后一张
    if(nextX == (self.dataArray.count - 1) * DJ_width){
        //如果是最后一张，就启用动画效果
        [UIView animateWithDuration:0.2 animations:^{
            self.scrollView.contentOffset = CGPointMake(nextX, 0);
        } completion:^(BOOL finished) {
            //最后一张走完了，重新开始走第二张
            self.scrollView.contentOffset = CGPointMake(self->DJ_width, 0);
        }];
    }else{
        //如果不是最后一张的话 就正常显示下一张
        [UIView animateWithDuration:0.2 animations:^{
            self.scrollView.contentOffset = CGPointMake(nextX, 0);
        } completion:^(BOOL finished) {
            
        }];
    }
}

- (UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, DJ_width, DJ_height)];
        //滚动时的代理需要设置好
        _scrollView.delegate = self;
        //设置拖拽的范围，如果不设置的话，不能手动拖拽，但是定时器可以使用的
        _scrollView.contentSize = CGSizeMake(self.dataArray.count * DJ_width, DJ_height);
        //设置分页滚动效果
        _scrollView.pagingEnabled = YES;
        //设置能否滚动
        _scrollView.scrollEnabled = YES;
        //设置弹簧效果
        _scrollView.bounces = NO;
        //设置滚动的起始位置
        _scrollView.contentOffset = CGPointMake(DJ_width, 0);
        //滚动条
        _scrollView.showsVerticalScrollIndicator = NO;
        _scrollView.showsHorizontalScrollIndicator = NO;
        
        //设置滚动时，每张图片的位置,主要就是遍历本地数组，进行进行图片赋值
        for(int i = 0;i < self.dataArray.count; i++){
            //初始化图片，
            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(DJ_width * i, 0, DJ_width, DJ_height)];
            //给图片赋值
            imageView.image = [UIImage imageNamed:self.dataArray[i]];
            //图片是否可以交互 就是用户是否可以点击
            imageView.userInteractionEnabled = YES;
            
            //如果可以点击的话，就添加手势
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImage:)];
            [imageView addGestureRecognizer:tap];
            //将图片添加到scrollView上
            [_scrollView addSubview:imageView];
        }

    }
    return _scrollView;
}

#pragma mark - 手势方法
- (void)clickImage:(UITapGestureRecognizer *)tap{
    //代理方法 各种跳转
}


@end
